package com.hongfasoft.wx.service;

import net.sf.json.JSONArray;

import com.hongfasoft.util.DBManager;
import com.hongfasoft.util.TokenAccessManager;
import com.hongfasoft.util.WeixinUtil;
import com.hongfasoft.wx.pojo.Button;
import com.hongfasoft.wx.pojo.CommonButton;
import com.hongfasoft.wx.pojo.ComplexButton;
import com.hongfasoft.wx.pojo.Menu;
import com.hongfasoft.wx.pojo.ViewButton;

/**
 * 菜单管理器类
 * 
 * @author liufeng
 * @date 2013-08-08
 */
public class MenuManager
{

	public static void main(String[] args)
	{

		// 调用接口获取access_token
		AccessToken at = TokenAccessManager.getAccessToken();
		System.out.println(at.toString());
		if (null != at)
		{
			// 调用接口创建菜单
			int result = WeixinUtil.createMenu(getMenu(), at.getToken());

			System.out.println(JSONArray.fromObject(getMenu()));
			// 判断菜单创建结果
			if (0 == result) System.out.println("菜单创建成功！");
			else
				System.out.println("菜单创建失败，错误码：" + result);
		}
	}

	/**
	 * 组装菜单数据
	 * 
	 * @return
	 */
	private static Menu getMenu()
	{
		// 第一菜单
		// CommonButton btn11 = new CommonButton();
		// btn11.setName("最近十条");
		// btn11.setType("click");
		// btn11.setKey("11");
		//
		// CommonButton btn12 = new CommonButton();
		// btn12.setName("个股诊断");
		// btn12.setType("click");
		// btn12.setKey("12");
//
		CommonButton mainBtn1 = new CommonButton();
		mainBtn1.setName("资金流动论");
		mainBtn1.setType("click");
		mainBtn1.setKey("11");
		

//		ViewButton mainBtn1 = new ViewButton();
//		// CommonButton btn31 = new CommonButton();
//		mainBtn1.setName("资金流动论");
//		mainBtn1.setType("view");
//		mainBtn1.setUrl(DBManager.WEB_URL + "userArticle.html?menu=11");
		// mainBtn1.setSub_button(new CommonButton[] { btn11, btn12 });

		// 第二菜单
//		CommonButton btn21 = new CommonButton();
//		btn21.setName("独家案例");
//		btn21.setType("click");
//		btn21.setKey("21");
		ViewButton btn21 = new ViewButton();
		btn21.setName("独家案例");
		btn21.setType("view");
		btn21.setUrl(DBManager.WEB_URL + "userArticle.html?menu=21");

		ViewButton btn22 = new ViewButton();
		btn22.setName("独家观点");
		btn22.setType("view");
		btn22.setUrl(DBManager.WEB_URL + "userArticle.html?menu=22");

		// CommonButton btn23 = new CommonButton();
		// btn22.setName("会员体验");
		// btn22.setType("click");
		// btn22.setKey("23");

		ComplexButton mainBtn2 = new ComplexButton();
		mainBtn2.setName("独家");
		mainBtn2.setSub_button(new Button[] { btn22, btn21 });

		// 第三菜单
		ViewButton btn31 = new ViewButton();
		// CommonButton btn31 = new CommonButton();
		btn31.setName("会员须知");
		btn31.setType("view");
		btn31.setUrl(DBManager.WEB_URL + "memberIntroduce.html");
		// btn31.setKey("31");

		CommonButton btn32 = new CommonButton();
		btn32.setName("成为会员");
		btn32.setType("click");
		btn32.setKey("32");

		CommonButton btn33 = new CommonButton();
		btn33.setName("专享案例");
		btn33.setType("click");
		btn33.setKey("33");

		CommonButton btn34 = new CommonButton();
		btn34.setName("股票池");
		btn34.setType("click");
		btn34.setKey("34");

		ComplexButton mainBtn3 = new ComplexButton();
		mainBtn3.setName("个人中心");
		mainBtn3.setSub_button(new Button[] { btn31, btn32, btn33, btn34 });

		/**
		 * 这是公众号xiaoqrobot目前的菜单结构，每个一级菜单都有二级菜单项<br>
		 * 
		 * 在某个一级菜单下没有二级菜单的情况，menu该如何定义呢？<br>
		 * 比如，第三个一级菜单项不是“更多体验”，而直接是“幽默笑话”，那么menu应该这样定义：<br>
		 * menu.setButton(new Button[] { mainBtn1, mainBtn2, btn33 });
		 */
		Menu menu = new Menu();
		menu.setButton(new Button[] { mainBtn1, mainBtn2, mainBtn3 });

		return menu;
	}
}